{% extends "base.html" %}

{% block title %}我的签到{% endblock %}

{% block head %}
{{ super() }}
<link href="{{ static_url('my.css') }}" rel="stylesheet" type="text/css">
{% endblock %}

{% block body %}
{{ utils.header(current_user) }}
<script src="/static/misc.js"></script>
<section class="task", id="task section">
  <div class="container">
    <h2>
      我的签到
      <a href="/task/new" class="btn btn-default btn-xs modal_load glyphicon glyphicon-plus"></a>
      {% if adminflg %}
      <span type="span" class="btn btn-default">
        <a data-load-method="GET" class="modal_load" href="/user/{{ userid }}/manage" title="管理用户">管理用户</a>
      </span>
      <span type="span" class="btn btn-default">
        <a data-load-method="GET" class="modal_load" href="/site/{{ userid }}/manage" title="管理网站">管理网站</a>
      </span>
      {% endif %}
      <span type="span" class="btn btn-default">
        <a data-load-method="GET" class="modal_load" href="/user/{{ userid }}/database" title="备份数据库">数据库</a>
      </span>
      <span type="span" class="btn btn-default">
        <a data-load-method="GET" class="modal_load" href="/util/toolbox/{{ userid }}" title="工具箱">工具箱</a>
      </span>
    </h2>
    <div class="text-left">
      <span >
        <span type="span" class="btn btn-default">
          <a data-load-method="GET" class="modal_load" href="/task/{{ userid }}/multi?op=disable" title="禁用多个任务">禁用</a>
        </span>
        <span type="span" class="btn btn-default">
          <a data-load-method="GET" class="modal_load" href="/task/{{ userid }}/multi?op=enable" title="启用多个任务">启用</a>
        </span>
        <span type="span" class="btn btn-default">
          <a data-load-method="GET" class="modal_load" href="/task/{{ userid }}/multi?op=delete" title="启用多个任务">删除</a>
        </span>
        <span type="span" class="btn btn-default">
          <a data-load-method="GET" class="modal_load" href="/task/{{ userid }}/multi?op=settime" title="定时多个任务">定时</a>
        </span>
        <span type="span" class="btn btn-default">
          <a data-load-method="GET" class="modal_load" href="/task/{{ userid }}/multi?op=setgroup" title="分组多个任务">分组</a>
        </span>
      </span>
    </div>
    <table class="table table-hover">
      <thead>
        <tr>
          <th title="签到网站平台名称">任务名称</th>
          <th style="text-align:center" title="签到成功/失败次数">成/败</th>
          <th title="上次签到成功时间">上次成功</th>
          <th style="text-align:center" title="签到状态" nowrap="nowrap" >状态</th>
          <th title="预计下次签到时间">下次签到</th>
          <th style="text-align:center" title="签到操作区">操作</th>
        </tr>
      </thead>
      <tbody>
        {% for group in taskgroups | sort(reverse=True) %}
        <tr name="{{group}} taskgroups" onClick="groups_click(this)" title="{{group}} taskgroup">
          <tr>
            <td colspan="6" onClick="groups_click(this)" title="{{group}} taskgroup"  style="background-color:rgb(215,237,251);">
              <text>{{group}}</text> 
            </td> 
          </tr>
          
          {% for task in tasks | sort(reverse=True, attribute="ctime") %}
          {% if group == task.groups %}
          <tr class="{{group}} taskgroup ">
            <td>
              <input type="checkbox"  name="{{task.id}}" onclick="taskonclick(this)">
              {% if task.disabled %}<span title="任务处于禁用状态,使用 (立即执行) 成功签到一次,即可解除." style="color:#ff7600;border-left:5px solid #ff7600;padding-left:5px;">[禁用]</span>{% endif %}
              {{ utils.tplname(task.tpl) if task.tpl else "[已删除]" }}

              {% if task.note %}
              -
                <code>
                  {% if task.note | length > 15 %}
                <span data-toggle="popover" data-content="{{ task.note }}">{{ task.note | truncate(10, True) }}</span>
                {% else %} {{ task.note }} {% endif %}
                  {% endif %}
                </code>
            </td>
            <td style="text-align:center;color: #cacaca;"><a style="color:#518cb9;" title="成功计次">{{ task.success_count }}</a> / <a style="color:#c15454;" title="失败计次">{{ task.failed_count }}</a></td>
            <td><span data-toggle="popover" data-content="{{ format_date(task.last_success,-8*60,True,False,True) if task.last_success else '从未' }}">{{ format_date(task.last_success) if task.last_success else '从未' }}</span></td>

            <td style="text-align:center">{{ my_status(task) }}</td>
            <!-- <td>{{ task.msg }}</td> -->
        
            <td><span data-toggle="popover" data-content="{{ format_date(task.next,-8*60,True,False,True) if task.next else '从未' }}">{{ format_date(task.next) if task.next else '从未' }}</span></td>

            <td style="text-align:center">
              <div class="btn-group btn-group-xs" role="group">
                {% if task.tpl %}
                <span type="span" class="btn btn-default"><a class="modal_load" href="/task/{{ task.id }}/edit" title="修改签到数据">修改</a></span>
                {% else %}
                <span type="span" class="btn btn-default"><a data-method="POST" href="/task/{{ task.id }}/del" data-confirm="是否要删除任务?">删除</a></span>
                {% endif %}
                <span type="span" class="btn btn-default"><a data-load-method="POST" class="modal_load" href="/task/{{ task.id }}/run" title="立即执行签到任务">执行</a></span>
                <span type="span" class="btn btn-default"><a href="/tpl/{{ task.tplid }}/edit" target="_blank" title="查看模板数据">模板</a></span>
                <span type="span" class="btn btn-default"><a data-load-method="GET" class="modal_load" href="/task/{{ task.id }}/settime" title="修改执行时间">定时</a></span>
                <span type="span" class="btn btn-default"><a href="/task/{{ task.id }}/log" target="_blank" title="查看执行日志">日志</a></span>
                <span type="span" class="btn btn-default"><a data-load-method="GET" class="modal_load" href="/task/{{ task.id }}/group" title="将任务分组">分组</a></span>
              </div>
            </td>
            
          </tr>
          {% endif %}
          {% endfor %}
        </tr> 
        {% endfor %}
      </tbody>
    </table>
    <div class="text-right">
      <a href="/tpls/public" class="btn btn-default">所有公开模板</a>
    </div>
  </div>
</section>

<section class="tpl" id="tplsection">
  <div class="container">
    <h2>
      我的模板
      <a href="/har/edit" class="btn btn-default btn-xs glyphicon glyphicon-plus" target="_blank"></a>
      <span type="span" class="btn btn-default"><a href="/subscribe/{{ userid }}/" target="_blank" title="查看公共模板">公共模板</a></span>
    </h2>
    <table class="table table-hover">
      <thead>
        <tr>
          <th title="签到模板名称">模板名称</th>
          <th title="模板创建时间">创建时间</th>
          <th title="模板最近修改时间">最近修改</th>
          <th title="模板最近执行成功时间">最近成功</th>
          <th style="text-align:center" title="模板操作区">操作</th>
        </tr>
      </thead>
      <tbody>
        {% for tplgroup in tplgroups | sort(reverse=True) %}
        <tr name="{{tplgroup}} tplgroups">
          <tr>
            <td colspan="5"  style="background-color:rgb(215,237,251);">
              <text>{{ tplgroup }}</text> 
              <label>
                <input type="checkbox"  name="{{ tplgroup }} tplgroups" onchange="checkboxOnclick(this)"> 
              </label>
            </td> 
          </tr>
          {% for tpl in tpls | sort(reverse=True, attribute="mtime") %}
          {% if tplgroup == tpl.groups %}
          <tr class="{{ tplgroup }} tplgroups" >
            <td>
              {% if  tpl.updateable == 1 %}
              <span type="span" class="btn btn-default">
                <a href="/tpl/{{ tpl.id }}/edit?tplurl={{ tpl.tplurl | urlencode }}?tpldata={{ tpl.tpldata | urlencode }}" target="_blank" title="更新模板">更新
                </a>
              </span>
              {% endif %}
              {{ utils.tplname(tpl) }}
      
              {% if tpl.fork %}<span>[fork]</span>{% endif %}-<kbd>{{ tpl.siteurl }}</kbd>
            </td>
  
            <td><span data-toggle="popover" data-content="{{ format_date(tpl.ctime,-8*60,True,False,True)}}">{{ format_date(tpl.ctime) }}</span></td>         
            <td><span data-toggle="popover" data-content="{{ format_date(tpl.mtime,-8*60,True,False,True)}}">{{ format_date(tpl.mtime) }}</span></td>
        
            <td>
              {% if tpl.last_success %}
              <span class="last_success" data-toggle="popover" data-content="{{ format_date(tpl.last_success,-8*60,True,False,True)}} ">{{ format_date(tpl.last_success) }}</span>
              {% else %}
              从未
              {% endif %}
            </td>
  
            <td style="text-align:center">
              <div class="btn-group btn-group-xs" role="group" aria-label="...">
                {% if tpl.lock %}
                [锁定]
                {% else %}
                <span type="span" class="btn btn-default"><a href="/tpl/{{ tpl.id }}/edit" target="_blank" title="编辑模板数据">编辑</a></span>
                <span type="span" class="btn btn-default"><a data-method="POST" href="/tpl/{{ tpl.id }}/del" data-confirm="是否要删除模板?" title="删除模板数据">删除</a></span>
                {% endif %}
                <span type="span" class="btn btn-default"><a class="modal_load" href="/task/new?tplid={{ tpl.id }}" title="通过此模板新建签到">新建</a></span>
                <span type="span" class="btn btn-default"><a class="modal_load" href="/tpl/{{ tpl.id }}/push" title="发布自己创建的模板">发布</a></span>
                <span type="span" class="btn btn-default"><a data-load-method="GET" class="modal_load" href="/tpl/{{ tpl.id }}/group" title="将模板分组">分组</a></span>
              </div>
            </td>
          
          {% endif %}
          {% endfor %}
        </tr>
        {% endfor %}
      </tbody>
    </table>
    <div class="text-right">
      <a href="/pushs" class="btn btn-default">我的发布请求</a>
    </div>
  </div>
</section>

<script>
  $(function() {
    $('[data-toggle=popover]').popover({
      trigger: 'hover'
    });
  })
</script>

<script>
  function checkboxOnclick(checkbox){
    var tasknodes = document.getElementsByClassName(checkbox.name);
    for (i = 0; i < tasknodes.length; i++) { 
      if (checkbox.checked == true){
        $(tasknodes[i]).hide();
      }else{
        $(tasknodes[i]).show();
      }
    }
  }
</script>
<script src="/static/mutil_op.js"></script>
<script>
function Del_tasks () {
  var Indexs = {};
  var es = document.querySelectorAll('tr[class*=taskgroup]');
  es.forEach(function(entry){
    var tmp = entry.getElementsByTagName('td')[0].getElementsByTagName('input')[0]
    if (tmp.checked){
      Indexs[tmp.name] = ''
    }
  });
  var $this = $(this);
  var data = {taskids: JSON.stringify(Indexs), func: "Del"}
  $.ajax('/tasks/{{ userid }}', {
      type: 'POST',
      data: data,
    })
    .done(function(data) {
      goto_my()
    })
    .fail(function(jxhr) {
      $("del_confirm-result").html('<h1 class="alert alert-danger text-center">失败</h1><div class="well"></div>').show().find('div').text(jxhr.responseText);
    })
    .always(function() {
      $this.button('reset');});

  return false;
}
function setTasksGroup() {
    var Indexs = {};
    var es = document.querySelectorAll('tr[class*=taskgroup]');
    var tmp = ""
    var groupValue = ''
    es.forEach(function(entry){
    var tmp = entry.getElementsByTagName('td')[0].getElementsByTagName('input')[0]
    if (tmp.checked){
        Indexs[tmp.name] = ''
    }
    });
    
    tmp = document.querySelectorAll('#NewGroupValue')[0]
    if(tmp.value == ''){
    es = document.querySelectorAll('#oneCheck input[type=checkbox]');
    es.forEach(function (e){
        if (e.checked) {
        groupValue = e.name;
        }
    });
    }
    else {
    groupValue = tmp.value
    }
    var $this = $(this);
    var data = {taskids: JSON.stringify(Indexs), func: "setGroup", groupValue: groupValue}
    $.ajax('/tasks/{{ userid }}', {
        type: 'POST',
        data: data,
    })
    .done(function(data) {
        window.location.replace("/my/")
    })
    .fail(function(jxhr) {
        $('#run-result').html('<h1 class="alert alert-danger text-center">失败</h1><div class="well"></div>').show().find('div').text(jxhr.responseText);
    })
    .always(function() {
        $this.button('reset');});

    return false;
}
</script>

<script>
  if (typeof(window.localStorage.Groups) != "undefined"){
    var groupsTmp = JSON.parse(window.localStorage.Groups)
    for (var key in groupsTmp){
      var tasknodes = document.getElementsByClassName(key);
      if (tasknodes.length > 0){
        for (i = 0; i < tasknodes.length; i++) { 
            if (groupsTmp[key]){
            $(tasknodes[i]).hide();
            }else{
            $(tasknodes[i]).show();
            }
      }
      }
    }
  }

  if (typeof(sessionStorage.selectedtask) != "undefined"){
    var selectedtaskTmp = JSON.parse(sessionStorage.selectedtask)
    for (var key in selectedtaskTmp){
      var tasknodes = document.getElementsByName(key);
      if (tasknodes.length > 0){
        tasknodes[0].checked = selectedtaskTmp[key]
      }
    }
  }
</script>

<script>
  function taskonclick(checkbox){
    var tmp = {}
    if (typeof(sessionStorage.selectedtask) != "undefined") {
      tmp = JSON.parse(sessionStorage['selectedtask'])
    } 
    tmp[checkbox.name] = checkbox.checked;
    sessionStorage['selectedtask'] = JSON.stringify(tmp);
  }
</script>

{% include 'LeftFuncBtn.html' %}
{{ utils.modal_load() }}
{{ utils.apost() }}
{% endblock %}
